
***********CALCULATE MONTHLY S&P CONVEXITY*************
est clear

use "$jfqa_rep/crsp_daily_index.dta", clear

qui{
drop if missing(spindx) 

sort year month day

gen monthyear=ym(year, month)

*assign first and last observation of month
gen prc_first=spindx if month~=month[_n-1]
egen min_prc=min(prc_first), by(year month)
replace prc_first=min_prc if missing(prc_first)
drop min_prc

gen prc_last=spindx if month~=month[_n+1]
egen min_prc=min(prc_last), by(year month)
replace prc_last=min_prc if missing(prc_last)
drop min_prc

sort year month
egen avgprc=mean(spindx), by(year month)
egen stdprc=sd(spindx), by(year month)
gen convexity=((prc_first+prc_last)/2-avgprc)/((prc_first+prc_last)/2)

drop if monthyear==monthyear[_n+1]

keep month year convexity
}

save "$jfqa_rep/T1_SP_Convexity.dta", replace


******PRODUCE TABLE 1A ESTIMATES*****
import delimited "$jfqa_rep/Goyal_Excel_Monthly.csv", clear

qui{
rename index sp_index 
rename d12 sp_div
rename e12 sp_earn

drop if missing(sp_index)

destring sp_index, force replace

merge 1:1 year month using "$jfqa_rep/CRSP_Monthly_Index.dta"
keep if _merge==3
drop _merge

gen monthyear=ym(year, month)
tsset monthyear
sort monthyear

gen log_prem=(crsp_spvw-log(1+rf))*100 

merge 1:1 year month using "$jfqa_rep/T1_SP_Convexity.dta"
keep if _merge==3
drop _merge
sort monthyear

rm "$jfqa_rep/T1_SP_Convexity.dta"

order year month monthyear log_prem

*construct control variables
gen dp = log(sp_div/sp_index)
gen dy = log(sp_div/L.sp_index)
gen ep = log(sp_earn/sp_index)
gen de = log(sp_div/sp_earn)
replace svar=svar
replace bm=bm
replace ntis=ntis
*gen beta_prem=csp
replace tbl=tbl
replace lty=lty
replace ltr=ltr
gen tms=lty-tbl
gen dfy = aaa-baa
gen dfr=corpr-ltr
replace infl=infl

drop if year<1963
drop if year==1963 & month<6

foreach i in convexity ntis tbl lty infl {
	replace `i'=-`i'
}

*standardized variables
foreach i in convexity log_prem dp dy ep de bm svar ntis tbl lty ltr tms dfy dfr infl {
egen `i'_std=std(`i') 
}


sort monthyear

foreach i in convexity log_prem dp dy ep de bm svar ntis tbl lty ltr tms dfy dfr infl {
	/*to get coefficients and t-stats*/ 
	eststo: newey log_prem L.`i'_std, lag(6)
	
	/*to get R-squareds*/ 
	eststo: reg   log_prem L.`i'_std

}
}

esttab using "$jfqa_rep/Table_1A.csv",  r(4) replace starlevels( * 0.20 ** 0.10 *** 0.02) b(2)
